const connection = require('../app/database');
const {APP_HOST, APP_PORT} = require('../app/config');

class ProfileService {

  // 上传用户资料
  async create(userId, gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name) {
    try {
      const statement = `INSERT INTO profile (user_id, gender, phone, qq, wx, email, suggest, website, address, emotion, birth,name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)`;
      const result = await connection.execute(statement, [userId, gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // (SELECT JSON_ARRAYAGG(JSON_OBJECT('id', w.id, 'wall', CONCAT('${APP_HOST}:', '${APP_PORT}', '/wall/', w.filename))) FROM wall w WHERE w.user_id = u.id) walls,
  // 获取用户资料
  async profile(userId) {
    try {
      const statement = `
                  SELECT
                    p.id id,
                    JSON_OBJECT('gender', p.gender, 'suggest', p.suggest, 'phone', p.phone, 'qq', p.qq, 'wx', p.wx, 'email', p.email, 'website', p.website, 'address', p.address, 'emotion', p.emotion, 'name', p.name, 'birth', p.birth) baseProfile,
                    JSON_OBJECT('id', u.id, 'nickname', u.nickname, 'avatarUrl', u.avatar_url, 'coverUrl', u.cover_url, 'isPrivate', u.private, 'creed', u.creed, 'createTime', u.createAt, 'updateTime', u.updateAt) user,
                    JSON_ARRAYAGG(JSON_OBJECT('id', us.id, 'socialId', us.social_id, 'socialKey', (SELECT social_key FROM social_type st WHERE st.id = us.social_id), 'socialValue', us.social_value, 'socialLink', us.social_link)) social,
                    (SELECT JSON_ARRAYAGG(JSON_OBJECT('id', i.id, 'indivKey', i.indiv_key, 'indivValue', i.indiv_value, 'mini', i.mini)) FROM indiv i WHERE i.user_id = u.id) individual,
                  p.createAt createTime,
                  p.updateAt updateTime
                  FROM user u
                  LEFT JOIN profile p ON u.id = p.user_id
                  LEFT JOIN user_social us ON u.id = us.user_id
                  WHERE u.id = ?
                  GROUP BY u.id`;
      const [result] = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 修改用户资料
  async update(userId, gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name) {
    try {
      const statement = `
      UPDATE profile 
      SET 
      gender = ?,
      phone = ?,
      qq = ?,
      wx = ?,
      email = ?,
      suggest = ?,
      website = ?,
      address = ?,
      emotion = ?,
      birth = ?,
      name = ?
      WHERE user_id = ?`;
      const result = await connection.execute(statement, [gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name, userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 验证用户是否有资料
  async isExistProfile(userId) {
    try {
      const statement = `SELECT id FROM profile WHERE user_id = ?`;
      const result = await connection.execute(statement, [userId]);
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

}

module.exports = new ProfileService();
